/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.apestar.modules.meibang.consultation.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.apestar.common.config.Global;
import cn.apestar.common.persistence.Page;
import cn.apestar.common.utils.StringUtils;
import cn.apestar.common.web.BaseController;
import cn.apestar.modules.meibang.consultation.entity.BeautyConsultationOrderRecommend;
import cn.apestar.modules.meibang.consultation.entity.BeautyConsultationOrderRecommendDetail;
import cn.apestar.modules.meibang.consultation.entity.CardResponse;
import cn.apestar.modules.meibang.consultation.entity.PackageResponse;
import cn.apestar.modules.meibang.consultation.entity.SearchApiRequest;
import cn.apestar.modules.meibang.consultation.enums.RecommendEnum;
import cn.apestar.modules.meibang.consultation.service.BeautyConsultationOrderRecommendDetailService;
import cn.apestar.modules.meibang.consultation.service.BeautyConsultationOrderRecommendService;
import cn.apestar.modules.meibang.consultation.service.OrderService;
import cn.apestar.modules.meibang.consultation.vo.CardVo;
import cn.apestar.modules.meibang.consultation.vo.PackageVo;
import cn.apestar.modules.meibang.consultation.vo.ProductVo;

/**
 * recommendController
 * @author recommend
 * @version 2017-06-06
 */
@Controller
@RequestMapping(value = "${adminPath}/consultation/recommend")
public class BeautyConsultationOrderRecommendController extends BaseController {

    @Autowired
    private BeautyConsultationOrderRecommendService beautyConsultationOrderRecommendService;

    @Autowired
    private BeautyConsultationOrderRecommendDetailService beautyConsultationOrderRecommendDetailService;

    @Autowired
    private OrderService orderService;

    @ModelAttribute
    public BeautyConsultationOrderRecommend get(@RequestParam(required = false) String answerId) {
        BeautyConsultationOrderRecommend entity = null;
        if (StringUtils.isNotBlank(answerId)) {
            entity = beautyConsultationOrderRecommendService.get(answerId);
        }
        if (entity == null) {

            // System.out.println(111);

            entity = new BeautyConsultationOrderRecommend();

        }
        return entity;
    }

    @RequestMapping(value = {"list" , ""})
    public String list(BeautyConsultationOrderRecommend beautyConsultationOrderRecommend, HttpServletRequest request, HttpServletResponse response,
        Model model) {
        Page<BeautyConsultationOrderRecommend> page = beautyConsultationOrderRecommendService.findPage(new Page<BeautyConsultationOrderRecommend>(
            request, response), beautyConsultationOrderRecommend);
        model.addAttribute("page", page);
        return "modules/recommend/beautyConsultationOrderRecommendList";
    }

    @RequestMapping(value = "form")
    public String form(BeautyConsultationOrderRecommend beautyConsultationOrderRecommend, Model model) {

        System.out.println(beautyConsultationOrderRecommend.getAnswerId());

        if (beautyConsultationOrderRecommend.getAnswerId() != null && beautyConsultationOrderRecommend.getAnswerId().length() > 0) {

            BeautyConsultationOrderRecommendDetail bcord = new BeautyConsultationOrderRecommendDetail();

            bcord.setAnswerId(beautyConsultationOrderRecommend.getAnswerId());

            List<BeautyConsultationOrderRecommendDetail> list = beautyConsultationOrderRecommendDetailService.findList(bcord);

            beautyConsultationOrderRecommend.setProductId("");

            beautyConsultationOrderRecommend.setProduct("");

            beautyConsultationOrderRecommend.setTreat("");

            beautyConsultationOrderRecommend.setTreatId("");

            beautyConsultationOrderRecommend.setService("");

            beautyConsultationOrderRecommend.setServiceId("");

            for (BeautyConsultationOrderRecommendDetail ord : list) {

                if (RecommendEnum.SERVICE.getValue().equals(ord.getRecommendType())) {

                    beautyConsultationOrderRecommend.setServiceId(beautyConsultationOrderRecommend.getServiceId() + "," + ord.getProductId());

                    beautyConsultationOrderRecommend.setService(beautyConsultationOrderRecommend.getService() + "," + ord.getProductName());

                }

                if (RecommendEnum.TREAT.getValue().equals(ord.getRecommendType())) {

                    beautyConsultationOrderRecommend.setTreatId(beautyConsultationOrderRecommend.getTreatId() + "," + ord.getProductId());

                    beautyConsultationOrderRecommend.setTreat(beautyConsultationOrderRecommend.getTreat() + "," + ord.getProductName());

                }

                if (RecommendEnum.PRODUCT.getValue().equals(ord.getRecommendType())) {

                    beautyConsultationOrderRecommend.setProductId(beautyConsultationOrderRecommend.getProductId() + "," + ord.getProductId());

                    beautyConsultationOrderRecommend.setProduct(beautyConsultationOrderRecommend.getProduct() + "," + ord.getProductName());

                }

            }

            if (beautyConsultationOrderRecommend.getService().length() > 0) {

                beautyConsultationOrderRecommend.setService(beautyConsultationOrderRecommend.getService().substring(1));

                beautyConsultationOrderRecommend.setServiceId(beautyConsultationOrderRecommend.getServiceId().substring(1));
            }

            if (beautyConsultationOrderRecommend.getTreat().length() > 0) {

                beautyConsultationOrderRecommend.setTreat(beautyConsultationOrderRecommend.getTreat().substring(1));

                beautyConsultationOrderRecommend.setTreatId(beautyConsultationOrderRecommend.getTreatId().substring(1));
            }

            if (beautyConsultationOrderRecommend.getProduct().length() > 0) {

                beautyConsultationOrderRecommend.setProduct(beautyConsultationOrderRecommend.getProduct().substring(1));

                beautyConsultationOrderRecommend.setProductId(beautyConsultationOrderRecommend.getProductId().substring(1));
            }

        }

        // System.out.println(beautyConsultationOrderRecommend.getOrderId());

        model.addAttribute("beautyConsultationOrderRecommend", beautyConsultationOrderRecommend);
        return "modules/meibang/consultation/beautyConsultationOrderRecommendForm";

    }

    @RequestMapping(value = "save")
    public String save(BeautyConsultationOrderRecommend beautyConsultationOrderRecommend, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, beautyConsultationOrderRecommend)) {
            return form(beautyConsultationOrderRecommend, model);
        }

        System.out.println(beautyConsultationOrderRecommend.getOrderId());

        beautyConsultationOrderRecommendService.saveRecommend(beautyConsultationOrderRecommend);

        BeautyConsultationOrderRecommendDetail bcord = new BeautyConsultationOrderRecommendDetail();

        ;

        bcord.setRecommendId(beautyConsultationOrderRecommend.getId());

        beautyConsultationOrderRecommendDetailService.realDelete(bcord);

        if (beautyConsultationOrderRecommend.getService() != null && beautyConsultationOrderRecommend.getService().length() > 0) {

            bcord.setRecommendId(beautyConsultationOrderRecommend.getId());

            bcord.setRecommendType(RecommendEnum.SERVICE.getValue());

            bcord.setProductName(beautyConsultationOrderRecommend.getService());

            bcord.setProductId(beautyConsultationOrderRecommend.getServiceId());

            bcord.setCreateDate(new Date());

            String[] serviceNames = beautyConsultationOrderRecommend.getService().split(",");

            String[] serviceIds = beautyConsultationOrderRecommend.getServiceId().split(",");

            // System.out.println(productNames.length + "," + productIds.length);

            for (int i = 0; i < serviceNames.length; i++) {

                bcord.setProductId(serviceIds[i]);

                bcord.setProductName(serviceNames[i]);

                bcord.setProductId(serviceIds[i]);

                bcord.setProductName(serviceNames[i]);

                bcord.setOrderId(beautyConsultationOrderRecommend.getOrderId());

                bcord.setAnswerId(beautyConsultationOrderRecommend.getAnswerId());

                bcord.setDelFlag("1");

                beautyConsultationOrderRecommendDetailService.insert(bcord);

            }

        }

        if (beautyConsultationOrderRecommend.getTreat() != null && beautyConsultationOrderRecommend.getTreat().length() > 0) {

            bcord.setRecommendId(beautyConsultationOrderRecommend.getId());

            bcord.setRecommendType(RecommendEnum.TREAT.getValue());

            bcord.setProductName(beautyConsultationOrderRecommend.getTreat());

            bcord.setProductId(beautyConsultationOrderRecommend.getTreatId());

            bcord.setCreateDate(new Date());

            String[] treatNames = beautyConsultationOrderRecommend.getTreat().split(",");

            String[] treatIds = beautyConsultationOrderRecommend.getTreatId().split(",");

            System.out.println(beautyConsultationOrderRecommend.getTreatId() + "+" + beautyConsultationOrderRecommend.getTreat());

            for (int i = 0; i < treatNames.length; i++) {

                bcord.setProductId(treatIds[i]);

                bcord.setProductName(treatNames[i]);

                bcord.setProductId(treatIds[i]);

                bcord.setProductName(treatNames[i]);

                bcord.setOrderId(beautyConsultationOrderRecommend.getOrderId());

                bcord.setAnswerId(beautyConsultationOrderRecommend.getAnswerId());

                bcord.setDelFlag("1");

                beautyConsultationOrderRecommendDetailService.insert(bcord);

            }

        }

        if (beautyConsultationOrderRecommend.getProduct() != null && beautyConsultationOrderRecommend.getProduct().length() > 0) {

            bcord.setRecommendId(beautyConsultationOrderRecommend.getId());

            bcord.setRecommendType(RecommendEnum.PRODUCT.getValue());

            bcord.setProductName(beautyConsultationOrderRecommend.getProduct());

            bcord.setCreateDate(new Date());

            bcord.setProductId(beautyConsultationOrderRecommend.getProductId());

            String[] productNames = beautyConsultationOrderRecommend.getProduct().split(",");

            String[] productIds = beautyConsultationOrderRecommend.getProductId().split(",");

            System.out.println(productNames.length + "," + productIds.length);

            for (int i = 0; i < productNames.length; i++) {

                bcord.setProductId(productIds[i]);

                bcord.setProductName(productNames[i]);

                bcord.setProductId(productIds[i]);

                bcord.setProductName(productNames[i]);

                bcord.setOrderId(beautyConsultationOrderRecommend.getOrderId());

                bcord.setAnswerId(beautyConsultationOrderRecommend.getAnswerId());

                bcord.setDelFlag("1");

                beautyConsultationOrderRecommendDetailService.insert(bcord);

            }

        }

        addMessage(redirectAttributes, "保存成功");
        return "redirect:" + Global.getAdminPath() + "/consultation/question/questionList?orderId=" + beautyConsultationOrderRecommend.getOrderId();
    }

    /**
     * 选择服务
     * @param role
     * @param model
     * @return
     */

    @RequestMapping(value = "productList")
    public String product(SearchApiRequest searchApiRequest, HttpServletRequest request, HttpServletResponse response, Model model) {

        Page<HashMap> page = new Page();

        // System.out.println(request.getParameter("pageSize"));

        if (request.getParameter("pageNo") != null) {

            searchApiRequest.setPage(Integer.parseInt(request.getParameter("pageNo")));

        } else {

            searchApiRequest.setPage(1);

        }

        searchApiRequest.setSize(10);

        Page pageResponse = orderService.findProductList(searchApiRequest);

        ProductVo pv = new ProductVo();

        HashMap map = pageResponse.getPage();

        page.setList(pageResponse.getList());
        page.setCount(Long.valueOf(map.get("totalRecord") + ""));
        page.setPageNo((Integer) map.get("currentIndex"));
        page.setPageSize((Integer) map.get("currentRecord"));
        page.initialize();

        model.addAttribute("page", page);

        model.addAttribute("pv", pv);

        // model.addAttribute("selectIds", Collections3.extractToString(userList, "name", ","));
        // model.addAttribute("officeList", officeService.findAll());
        return "modules/meibang/consultation/productList";
    }

    /**
     * 疗程服务
     * @param role
     * @param model
     * @return
     */

    @RequestMapping(value = "treatList")
    public String treat(SearchApiRequest searchApiRequest, HttpServletRequest request, HttpServletResponse response, Model model) {

        if (request.getParameter("pageNo") != null) {

            searchApiRequest.setPage(Integer.parseInt(request.getParameter("pageNo")));

        } else {

            searchApiRequest.setPage(1);

        }

        searchApiRequest.setSize(10);

        Page<HashMap> page = new Page();

        PackageResponse packageResponse = orderService.findTreatList(searchApiRequest);

        PackageVo pv = new PackageVo();

        HashMap map = packageResponse.getPage();

        // page.setList(packageResponse.getList());
        page.setCount(Long.valueOf(map.get("totalRecord") + ""));
        page.setPageNo((Integer) map.get("currentIndex"));
        page.setPageSize((Integer) map.get("currentRecord"));
        page.initialize();

        for (PackageVo pv1 : packageResponse.getList()) {

            System.out.println(pv1.getId());
        }

        model.addAttribute("page", page);

        model.addAttribute("treatList", packageResponse.getList());

        model.addAttribute("pv", pv);

        // model.addAttribute("selectIds", Collections3.extractToString(userList, "name", ","));
        // model.addAttribute("officeList", officeService.findAll());
        return "modules/meibang/consultation/treatList";
    }

    /**
     * 服务服务
     * @param role
     * @param model
     * @return
     */

    @RequestMapping(value = "serviceList")
    public String service(SearchApiRequest searchApiRequest, HttpServletRequest request, HttpServletResponse response, Model model) {

        if (request.getParameter("pageNo") != null) {

            searchApiRequest.setPage(Integer.parseInt(request.getParameter("pageNo")));

        } else {

            searchApiRequest.setPage(1);

        }

        searchApiRequest.setSize(10);

        Page<HashMap> page = new Page();

        CardResponse cardResponse = orderService.findServiceList(searchApiRequest);

        CardVo cv = new CardVo();

        HashMap map = cardResponse.getPage();

        // page.setList(packageResponse.getList());
        page.setCount(Long.valueOf(map.get("totalRecord") + ""));
        page.setPageNo((Integer) map.get("currentIndex"));
        page.setPageSize((Integer) map.get("currentRecord"));
        page.initialize();

        System.out.println(page.getCount() + "," + page.getPageNo() + "," + page.getPageSize());

        model.addAttribute("page", page);

        model.addAttribute("serviceList", cardResponse.getList());

        model.addAttribute("cv", cv);

        // model.addAttribute("selectIds", Collections3.extractToString(userList, "name", ","));
        // model.addAttribute("officeList", officeService.findAll());
        return "modules/meibang/consultation/serviceList";
    }

    @RequestMapping(value = "delete")
    public String delete(BeautyConsultationOrderRecommend beautyConsultationOrderRecommend, RedirectAttributes redirectAttributes) {
        beautyConsultationOrderRecommendService.delete(beautyConsultationOrderRecommend);
        addMessage(redirectAttributes, "删除成功");
        return "redirect:" + Global.getAdminPath() + "/consultation/recommend?repage";
    }

}